<template>
  <div class="app-container">
    <el-row>
      <!-- 顶部搜索栏 -->
      <el-form ref="queryForm" :model="queryParams" v-show="showSearch" :inline="true">
        <el-form-item label="图案名称" prop="productName">
          <el-input v-model="queryParams.patternName" placeholder="请输入图案名称" clearable  style="width: 240px">
          </el-input>
        </el-form-item>
        <el-form-item label="是否使用" prop="is_use">
          <el-select v-model="queryParams.is_use" placeholder="请选择是否使用" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in is_useOptions" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表头按钮组 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>

      <!-- 后台回显表格 -->
      <el-table v-loading="loading" :data="patternList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="图案序号" align="center" key="patternId" prop="patternId" v-if="columns[0].visible"  />
        <el-table-column label="图案名称" align="center" key="patternName" prop="patternName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
        <el-table-column label="图片" align="center" width="150px" key="patternImgUrl" prop="patternImgUrl" v-if="columns[2].visible" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <img v-bind:src="imgurl + scope.row.patternImgUrl" style="max-width: 100%;height: 30px;">
          </template>
        </el-table-column>
        <el-table-column label="上传人" align="center" key="uploadUser" prop="uploadUser" v-if="columns[3].visible" :show-overflow-tooltip="true" />
        <el-table-column label="上传日期" align="center" prop="uploadDate" v-if="columns[4].visible" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.uploadDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更改人" align="center" key="modifyUser" prop="modifyUser" v-if="columns[5].visible" width="120" />
        <el-table-column label="更改日期" align="center" prop="modifyDate" v-if="columns[6].visible" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.modifyDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否使用" align="center" key="is_use" prop="is_use" v-if="columns[8].visible" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <p v-if="scope.row.is_use=='0'">使用</p>
            <p v-if="scope.row.is_use=='1'">未使用</p>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope" v-if="scope.row.productId !== 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部pagination -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="图案名称" prop="productName">
              <el-input v-model="form.patternName" placeholder="请输入图案名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="上传图片" >
              <el-upload
                action="#"
                list-type="picture-card"
                accept=".jpg,.jpeg,.png,.JPG,.JPEG"
                :http-request="requestUpload"
                :before-upload="beforeUpload"
                :on-remove="handleRemove"
                :limit="1"
                :on-exceed="handleExceed"
                ref="upload">
                <i class="el-icon-upload el-icon--right"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="this.options.is_show != true">
            <img v-bind:src="this.options.img" style="width: 148px;height: 148px;">
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPattern, exportPattern, getPattern, addPattern, updatePattern, delPattern } from '@/api/wire/pap/pattern/pattern'

export default {
  name: "Pattern",
  data() {
    return {
      //图片地址前缀
      imgurl:process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      patternNames:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //产品数据
      patternList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      //存放上传的图片信息
      formData:new FormData(),
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patternName: undefined,
        is_use: undefined,
      },
      is_useOptions: [{
        "label": "是",
        "value": 0
      }, {
        "label": "否",
        "value": 1
      }],
      // 列信息
      columns: [
        { key: 0, label: `图案序号`, visible: true },
        { key: 1, label: `图案名称`, visible: true },
        { key: 2, label: `图案地址`, visible: true },
        { key: 3, label: `上传人`, visible: true },
        { key: 4, label: `上传日期`, visible: true },
        { key: 5, label: `更改人`, visible: true },
        { key: 6, label: `更改日期`, visible: true },
        { key: 7, label: `删除标记`, visible: true },
        { key: 8, label: `是否使用`, visible: true },
      ],
      rules: {
        patternName: [
          { required: true, message: "图案名称不能为空", trigger: "blur" }
        ],
      },
      options: {
        img:"",
        lifeImgs:[],
        is_show: false,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图案列表 */
    getList() {
      this.loading = true;
      listPattern(this.queryParams).then(response => {
          this.patternList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 表单重置
    reset() {
      this.form = {
        patternId: undefined,
        patternName: undefined,
        patternImgUrl: undefined,
        uploadUser: undefined,
        uploadDate: undefined,
        modifyUser: undefined,
        modifyDate: undefined,
        del_flag: undefined,
        is_use: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getPattern().then(response => {
        this.open = true;
        this.title = "添加图案";
      });
      this.options.is_show = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patternId = row.patternId || this.ids;
      getPattern(patternId).then(response => {
        this.form = response.data;
        this.options.img = process.env.VUE_APP_BASE_API + response.data.patternImgUrl;
        this.open = true;
        this.title = "修改图案";
      });
      this.options.is_show = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patternIds = row.patternId || this.ids;
      const patternNames = row.patternName || this.patternNames;

      this.$confirm('是否确认删除图案名称为"' + patternNames + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delPattern(patternIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有图案数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportPattern(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 多选框选中数据 **/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patternId);
      this.patternNames = selection.map(item => item.patternName);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 上传预处理
    beforeUpload(file) {
      if (file.type.indexOf("image/") == -1) {
        this.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
      } else {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => {
          //上传时自动替换图片
          // this.options.img = reader.result;
        };
      }
    },
    // 覆盖默认的上传行为
    requestUpload(params) {
      this.formData.append("patternfile", params.file);
      this.options.is_show = true;
    },
    //移除方法
    handleRemove(file,fileList){
      if (this.options.img != ""){
        //点击修改按钮时，触发移除方法
        this.options.is_show = false;
      } else {
        //点击新增按钮时，触发移除方法
        this.options.is_show = true;
      }
    },
    //上传超出限制
    handleExceed(files, fileList) {
      this.$message.warning(`请最多上传 1 个文件。`);
    },
    handleRemove1(file,fileList){
      this.fileList = fileList;
    },
    /** 新增/修改 对话框提交按钮 */
    submitForm: function() {
      console.log(this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.formData.get("patternfile") == null){
            this.formData.append("patternfile", "");
          }
          if(this.formData.get("patternJson") == null){
            this.formData.append("patternJson", JSON.stringify(this.form));
          }
          if (this.form.patternId != undefined) {
            updatePattern(this.formData).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPattern(this.formData).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 新增对话框取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
      this.options.img = "";
    },
  },
}
</script>

<style scoped>

</style>
